
#include "..\UnitTest\ut_features.h"
#include "..\UnitTest\ut_runner.h"

//#include "cx_platform_base.h"
#include "cx_vector.h"
#include "cx_memory.h"

static QINT Compare_Int (QVOID * pFirst, QVOID * pSecond)
{
	QINT* p1 = (QINT*)pFirst;
	QINT* p2 = (QINT*)pSecond;
	if (*p1 == *p2) return 0;
	else if (*p1 > *p2) return 1;
	else return -1;
}

/*************************************************************************
* UT CASE
*************************************************************************/

UT_CASE( __test_enter )
{
	return UT_RESULT_PASS;
}

UT_CASE( __cx_vector_new_test )
{
	QVector* vector = cx_vector_new(sizeof(QINT), 5);
	UT_EXPECT_OR_RETURN( vector != NULL, UT_RESULT_FAIL );

	if (vector != NULL)
	{
		cx_vector_releaseRef(vector);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_vector_count_test )
{
	QINT obj_i = 5;
	QVector* vector = QNULL;
	vector = cx_vector_new(sizeof(QINT), 5);
	UT_EXPECT_OR_RETURN( vector != NULL, UT_RESULT_FAIL );

	obj_i = 5;
	cx_vector_append(vector, &obj_i, sizeof(QINT));
	UT_EXPECT_OR_RETURN( vector->m_iCount == 1, UT_RESULT_FAIL );

	if (vector != NULL)
	{
		cx_vector_releaseRef(vector);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_vector_append_test )
{
	QINT obj_i = 5;
	QVector* vector = QNULL;
	QBOOL ret = QFALSE;
	
	vector = cx_vector_new(sizeof(QINT), 5);
	UT_EXPECT_OR_RETURN( vector != NULL, UT_RESULT_FAIL );

	ret = cx_vector_append(vector, &obj_i, sizeof(QINT));
	UT_EXPECT_OR_RETURN( ret == QTRUE, UT_RESULT_FAIL );

	if (vector != NULL)
	{
		cx_vector_releaseRef(vector);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_vector_removeAll_test )
{
	QINT obj_i = 5;	
	QVector* vector = QNULL;
	vector = cx_vector_new(sizeof(QINT), 5);
	UT_EXPECT_OR_RETURN( vector != NULL, UT_RESULT_FAIL );

	obj_i = 5;
	cx_vector_append(vector, &obj_i, sizeof(QINT));
	UT_EXPECT_OR_RETURN( vector->m_iCount == 1, UT_RESULT_FAIL );

	cx_vector_removeAll(vector);
	UT_EXPECT_OR_RETURN( vector->m_iCount == 0, UT_RESULT_FAIL );

	if (vector != NULL)
	{
		cx_vector_releaseRef(vector);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_vector_setElementAt_test )
{
	QINT obj_i = 5;
	QINT dsize = 0;
	QINT* obj_o = QNULL;
	QVector* vector = QNULL;
	
	vector = cx_vector_new(sizeof(QINT), 5);
	UT_EXPECT_OR_RETURN( vector != NULL, UT_RESULT_FAIL );

	cx_vector_append(vector, &obj_i, sizeof(QINT));
	UT_EXPECT_OR_RETURN( vector->m_iCount == 1, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_vector_getElementAt(vector, 0, &dsize);
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );

	obj_i = 6;
	cx_vector_setElementAt(vector, 0, &obj_i, sizeof(QINT));

	dsize = 0;
	obj_o = (QINT*)cx_vector_getElementAt(vector, 0, &dsize);
	UT_EXPECT_OR_RETURN( *obj_o == 6, UT_RESULT_FAIL );

	if (vector != NULL)
	{
		cx_vector_releaseRef(vector);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_vector_getElementAt_test )
{
	QINT obj_i = 5;
	QINT dsize = 0;
	QVector* vector = QNULL;
	QINT* obj_o = QNULL;
	
	vector = cx_vector_new(sizeof(QINT), 5);
	UT_EXPECT_OR_RETURN( vector != NULL, UT_RESULT_FAIL );

	cx_vector_append(vector, &obj_i, sizeof(QINT));
	UT_EXPECT_OR_RETURN( vector->m_iCount == 1, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_vector_getElementAt(vector, 0, &dsize);
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );
	if (vector != NULL)
	{
		cx_vector_releaseRef(vector);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_vector_insertElementAt_test )
{
	QINT obj_i = 5;
	QINT dsize = 0;
	QINT* obj_o = QNULL;
	QVector* vector = QNULL;
	vector = cx_vector_new(sizeof(QINT), 5);
	UT_EXPECT_OR_RETURN( vector != NULL, UT_RESULT_FAIL );

	cx_vector_append(vector, &obj_i, sizeof(QINT));
	UT_EXPECT_OR_RETURN( vector->m_iCount == 1, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_vector_getElementAt(vector, 0, &dsize);
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );

	obj_i = 6;
	cx_vector_insertElementAt(vector, 0, &obj_i, sizeof(QINT));
	UT_EXPECT_OR_RETURN( vector->m_iCount == 2, UT_RESULT_FAIL );

	dsize = 0;
	obj_o = (QINT*)cx_vector_getElementAt(vector, 0, &dsize);
	UT_EXPECT_OR_RETURN( *obj_o == 6, UT_RESULT_FAIL );

	dsize = 0;
	obj_o = QNULL;
	obj_o = (QINT*)cx_vector_getElementAt(vector, 1, &dsize);
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );

	if (vector != NULL)
	{
		cx_vector_releaseRef(vector);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __cx_vector_removeElementAt_test )
{
	QINT obj_i = 5;
	QINT dsize = 0;
	QINT* obj_o = QNULL;
	
	QVector* vector = cx_vector_new(sizeof(QINT), 5);
	UT_EXPECT_OR_RETURN( vector != NULL, UT_RESULT_FAIL );

	cx_vector_append(vector, &obj_i, sizeof(QINT));
	UT_EXPECT_OR_RETURN( vector->m_iCount == 1, UT_RESULT_FAIL );

	obj_i = 6;
	cx_vector_append(vector, &obj_i, sizeof(QINT));
	UT_EXPECT_OR_RETURN( vector->m_iCount == 2, UT_RESULT_FAIL );

	obj_o = (QINT*)cx_vector_getElementAt(vector, 0, &dsize);
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );

	dsize = 0;
	obj_o = (QINT*)cx_vector_getElementAt(vector, 1, &dsize);
	UT_EXPECT_OR_RETURN( *obj_o == 6, UT_RESULT_FAIL );

	cx_vector_removeElementAt(vector, 0, NULL);
	UT_EXPECT_OR_RETURN( vector->m_iCount == 1, UT_RESULT_FAIL );
	dsize = 0;
	obj_o = (QINT*)cx_vector_getElementAt(vector, 0, &dsize);
	UT_EXPECT_OR_RETURN( *obj_o == 6, UT_RESULT_FAIL );

	if (vector != NULL)
	{
		cx_vector_releaseRef(vector);
	}

	return UT_RESULT_PASS;
}

UT_CASE( __cx_vector_find_test )
{
	QINT obj_i = 5;
	QINT obj_target = 5;
	QINT* obj_o = NULL;
	QINT dsize = 0;
	QINT ret = 0;
	QVector* vector = QNULL;
	vector = cx_vector_new(sizeof(QINT), 5);
	UT_EXPECT_OR_RETURN( vector != NULL, UT_RESULT_FAIL );

	cx_vector_append(vector, &obj_i, sizeof(QINT));
	UT_EXPECT_OR_RETURN( vector->m_iCount == 1, UT_RESULT_FAIL );

	obj_i = 6;
	cx_vector_append(vector, &obj_i, sizeof(QINT));
	UT_EXPECT_OR_RETURN( vector->m_iCount == 2, UT_RESULT_FAIL );

	ret = cx_vector_find(vector, &obj_target, (void**)&obj_o, &dsize, Compare_Int);
	UT_EXPECT_OR_RETURN( ret == 0, UT_RESULT_FAIL );
	UT_EXPECT_OR_RETURN( *obj_o == 5, UT_RESULT_FAIL );
	UT_EXPECT_OR_RETURN( dsize == sizeof(QINT), UT_RESULT_FAIL );

	if (vector != NULL)
	{
		cx_vector_releaseRef(vector);
	}
	return UT_RESULT_PASS;
}

UT_CASE( __test_leave )
{
	return UT_RESULT_PASS;
}

/*************************************************************************
* UT GROUP
*************************************************************************/

UT_GROUP( AcceptanceTest_CX_VECTOR )
{
	UT_ADD_CASE( __test_enter ),
	UT_ADD_CASE( __cx_vector_new_test ),
	UT_ADD_CASE( __cx_vector_count_test ),
	UT_ADD_CASE( __cx_vector_append_test ),
	UT_ADD_CASE( __cx_vector_removeAll_test ),
	UT_ADD_CASE( __cx_vector_setElementAt_test ),
	UT_ADD_CASE( __cx_vector_getElementAt_test ),
	UT_ADD_CASE( __cx_vector_insertElementAt_test ),
	UT_ADD_CASE( __cx_vector_removeElementAt_test ),
	UT_ADD_CASE( __cx_vector_find_test ),
	UT_ADD_CASE( __test_leave ),

	UT_LAST_CASE
};
